﻿﻿chrome.browserAction.setTitle({
	title: chrome.i18n.getMessage("extName")
});

if(!localStorage["translate_to"]) {
	chrome.i18n.getAcceptLanguages(function (languages) {
		//console.log(languages);
		localStorage["translate_to"] = languages[0];
	});
}

if(!localStorage["always_on"]) {
	localStorage["always_on"] = false;
}

//For fixing chrome 5 bug
var completed = false;

chrome.tabs.onUpdated.addListener(
	function(tabId, changeInfo, tab) {
		if (localStorage["always_on"] == "true" && changeInfo.status == "loading") {
			completed = false;
		}
		
		if (localStorage["always_on"] == "true" && changeInfo.status == "complete" && !completed) {
//			console.log(tab.id);
			completed = true;
			chrome.tabs.executeScript(tabId ,{file: 'inject.js'}, function (){});
			chrome.browserAction.setBadgeText({"text": "on", "tabId": tabId});
		}
	}
);


chrome.browserAction.onClicked.addListener(function (tab) {
	var lang = localStorage["translate_to"];
	chrome.tabs.executeScript(tab.id, {file: 'inject.js'}, function (){});
	chrome.browserAction.setBadgeText({"text": "on", "tabId": tab.id});
});

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    //console.log(request.msg);
    sendResponse({lang: localStorage["translate_to"]});
  }
);